# -*- coding: utf-8 -*-
"""
Created on Thu Dec 10 15:25:37 2015

@author: liliangrong
"""

import urllib
import urllib2
import cookielib

filename = 'cookie_zhihu.txt'

#声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
cookie = cookielib.MozillaCookieJar(filename)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 5.1; rv:35.0) Gecko/20100101 Firefox/35.0'}
postdata = urllib.urlencode({
			'account':'liliangrong0@126.com',
			'password':'llr139248'
		})

#登录教务系统的URL
loginUrl = 'http://www.zhihu.com/'

#模拟登录，并把cookie保存到变量
result = opener.open(loginUrl,postdata,headers)

#保存cookie到cookie.txt中
cookie.save(ignore_discard=True, ignore_expires=True)

#利用cookie请求访问另一个网址，此网址是成绩查询网址
gradeUrl = 'http://www.zhihu.com/'

#请求访问成绩查询网址
result = opener.open(gradeUrl)

print result.read()